$(document).ready(function () {
    App.init();
    TableManageFixedHeader.init();
    $('.selectpicker').selectpicker({
        style: 'btn-white',
        size: 5
    });
    $('.datepicker-default').datepicker({
        language: 'zh-CN',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: "month",//设置只显示到月份
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: 'linked'//显示今日按钮
    });
    var tabType = $("#tabType").val();
    controlTabShow(tabType);
    selectPayInsurerOrderList();
    selectPayChannelOrderList();

    var datenum = new Date().getTime();
    var date = new Date(datenum)
    date1 = dateFtt("yyyy-MM-dd", date)
    $("#dateIssuance").val(date1);
});

//分页展示
var insurerOrdertable;
var channelOrdertable;
function selectPayInsurerOrderList() {
    insurerOrdertable = $("#insurerList").DataTable({
        "bAutoWidth": false,
        'ordering': false,
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [15],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "无匹配结果，请更换筛选条件再试",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "当前第 0 - 0 条　共计 0 条",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/ins/order/selectOrderList",
            "dataSrc": "aaData",
            "data": function (params) {
                if ($("#insurerId1 option:selected ").val() != 0) {
                    params.insurerId = $("#insurerId1 option:selected ").val();
                }
                if ($("#themeKey option:selected ").val() != 0) {
                    params.themeKey = $("#themeKey option:selected ").val();
                }
                params.plateNum = $("#plateNum1").val();
                params.channelIdStr = $("#channelId").val();
                var orderStatusVal = $("#orderStatus option:selected ").val();
                if (orderStatusVal != 0) {
                    params.orderStatus = orderStatusVal;
                } else {
                    params.orderStatus = "1,2,3,4,5,6,9,10,14";
                }
                params.opponentType = 1;
            }
        },
        "columns": [
            {
                "data": "ordCode",
                "render": function (data, type, row, meta) {
                    return "<a href='#tab3' onclick='showOrderInfo(" + row.tid + ")' >" + row.ordCode + "</a>"
                },
                "bSortable": false
            },
            {
                "data": function (row, type, set) {
                    if (row.insyrerCompany == null || row.insyrerCompany.name == null || row.insyrerCompany.name == '') {
                        return "--"
                    } else {
                        return row.insyrerCompany.name;
                    }
                }
            },
            {
                "data": "paySubject",
                "render": function (data, type, row, meta) {
                    switch (data) {
                        case 1:
                            return "环宇康泰";
                        case 2:
                            return "青岛盛达";
                        case 3:
                            return "玉环天云";
                        case 4:
                            return "汉龙";
                        default :
                            return "--";
                    }
                },
                "bSortable": false
            },
            {
                "data": function (row, type, set) {
                    if (row.channel == null) {
                        return "--"
                    } else if (row.channel.type == 2) {
                        return row.channel.companyRegisterName;
                    } else if (row.channel.type == 1 && row.channel.contact != null) {
                        return row.channel.contact.name;
                    } else {
                        return "--"
                    }
                }
            },
            {
                "data": function (row, type, set) {
                    if (row.channel == null || row.channel.contact == null || row.channel.contact.telephone == null || row.channel.contact.telephone == '') {
                        return "--"
                    } else {
                        return row.channel.contact.telephone;
                    }
                }
            },
            {
                "data": "insurant",
                "defaultContent": '--'
            },
            {
                "data": "orgIdCode",
                "defaultContent": '--'
            },
            {
                "data": "plateNum",
                "defaultContent": '--'
            },
            {
                "data": "carOwner",
                "defaultContent": '--'
            },
            {
                "data": "typeInsurance",
                "render": function (data, type, row, meta) {
                    switch (data) {
                        case 0:
                            return "交强险";
                        case 1:
                            return "商业险";
                        default :
                            return "--";
                    }
                },
                "bSortable": false
            },
            {
                "data": "mercialPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "pulsoryPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "vechcleVesselTax",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "allPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "createDate",
                "defaultContent": '--'
            },
            {
                "data": "status",
                "render": function (data, type, row, meta) {
                    switch (data) {
                        case 1:
                            return "待审核";
                        case 2:
                            return "待支付";
                        case 3:
                            return "待完善保单";
                        case 4:
                            return "待渠道确认";
                        case 5:
                            return "渠道拒绝";
                        case 6:
                            return "审核驳回";
                        case 9:
                            return "支付失败-产品变更";
                        case 10:
                            return "支付失败-授信不足";
                        case 11:
                            return "审核失败-产品变更";
                        case 12:
                            return "审核失败-产品不可用";
                        case 14:
                            return "支付失败-产品不可用";
                        default :
                            return "--";
                    }
                },
                "bSortable": false
            }],
        "columnDefs": [{
            "targets": 16,//是操作按钮目标列
            "data": null,
            "render": function (data, type, row) {
                if (row.status == 1 || row.status == 5) {
                    return "<a href='/ins/order/findOrderCheckInfo?tid=" + row.tid + "' class='btn-link'>审核</a>";
                } else if (row.status == 2) {
                    return "<a href='/ins/order/findOrderPayInfo?tid=" + row.tid + "' class='btn-link'>支付</a>";
                } else if (row.status == 3) {
                    return "<a href='/ins/order/perfectOrder?ordCode=" + row.ordCode + "&tid=" + row.tid + "&insyrerId=" + row.insyrerId + "' class='btn-link'>完善保单</a>";
                } else {
                    return "--"
                }
            }
        }]
    });

}
function showAddTab() {
    $("#myTab li:eq(2)").addClass("active");
    $("#tab3").addClass("active in");
    $("#tab3").show();
    $("#myTab li:eq(1)").removeClass("active");
    $("#tab2").removeClass(" active in");
    $("#tab2").hide();
    $("#myTab li:eq(0)").removeClass("active");
    $("#tab1").removeClass(" active in");
    $("#tab1").hide();
}
function tabShowOrHide() {
    $("#tab1").hide();
    $("#tab2").hide();
    $("#tab3").show();
    $("#perfect").removeClass("hidden").trigger("click");
}
function showOrderInfo(orderId) {
    tabShowOrHide();
    $("#perfect").html("保单详情");
    $("#perfect").attr("tabType", "3");
    $("#iframe").attr("src", "/ins/order/orderInfoDetail?tid=" + orderId);
}
function selectPayChannelOrderList() {
    channelOrdertable = $("#channelList").DataTable({
        "bAutoWidth": false,
        'ordering': false,
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [15],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "无匹配结果，请更换筛选条件再试",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "当前第 0 - 0 条　共计 0 条",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/ins/order/selectOrderList",
            "dataSrc": "aaData",
            "data": function (params) {
                if ($("#insurerId2 option:selected ").val() != 0) {
                    params.insurerId = $("#insurerId2 option:selected ").val();
                }
                params.channelIdStr = $("#channelId").val();
                params.plateNum = $("#plateNum2").val();
                params.insurantStartDate = $("#insurantStartDate").val();
                params.insurantEndDate = $("#insurantEndDate").val();
                params.orderStatus = 7;
                params.investmentType = 1;
                params.recordStatus = 3;
                params.opponentType = 2;
            }
        },
        "columns": [
            {
                "data": "ordCode",
                "render": function (data, type, row, meta) {
                    return "<a href='#tab3' onclick='showOrderInfo(" + row.tid + ")' >" + row.ordNum + "</a>"
                },
                "bSortable": false
            },
            {
                "data": function (row, type, set) {
                    if (row.insyrerCompany == null || row.insyrerCompany.name == null || row.insyrerCompany.name == '') {
                        return "--"
                    } else {
                        return row.insyrerCompany.name;
                    }
                }
            },
            {
                "data": "paySubject",
                "render": function (data, type, row, meta) {
                    switch (data) {
                        case 1:
                            return "环宇康泰";
                        case 2:
                            return "青岛盛达";
                        case 3:
                            return "玉环天云";
                        case 4:
                            return "汉龙";
                        default :
                            return "--";
                    }
                },
                "bSortable": false
            },
            {
                "data": function (row, type, set) {
                    if (row.channel == null) {
                        return "--"
                    } else if (row.channel.type == 2) {
                        return row.channel.companyRegisterName;
                    } else if (row.channel.type == 1 && row.channel.contact != null) {
                        return row.channel.contact.name;
                    } else {
                        return "--"
                    }
                }
            },
            {
                "data": function (row, type, set) {
                    if (row.channel == null || row.channel.contact == null || row.channel.contact.telephone == null || row.channel.contact.telephone == '') {
                        return "--"
                    } else {
                        return row.channel.contact.telephone;
                    }
                }
            },
            {
                "data": "insurant",
                "defaultContent": '--'
            },
            {
                "data": "orgIdCode",
                "defaultContent": '--'
            },
            {
                "data": "plateNum",
                "defaultContent": '--'
            },
            {
                "data": "carOwner",
                "defaultContent": '--'
            },
            {
                "data": "typeInsurance",
                "render": function (data, type, row, meta) {
                    switch (data) {
                        case 0:
                            return "交强险";
                        case 1:
                            return "商业险";
                        default :
                            return "--";
                    }
                },
                "bSortable": false
            },
            {
                "data": "mercialPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "pulsoryPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "vechcleVesselTax",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "allPremium",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "createDate",
                "defaultContent": '--',
            },
            {
                "data": "channelRebate",
                "defaultContent": '--'
            },
            {
                "data": "mustRebateAmt",
                "defaultContent": '--',
                "sClass": "text-right"
            },
            {
                "data": "paydate",
                "defaultContent": '--'
            }
        ],
        "columnDefs": [{
            "targets": 18,//是操作按钮目标列
            "data": null,
            "render": function (data, type, row) {
                if (row.recStatus == 3) {
                    return "<a href='#' class='btn-link' onclick='settleChannelRebate(" + row.tid + "," + row.mustRebateAmt + ")'>结算</a>";
                } else {
                    return "--";
                }

            }
        }]
    });

}
// 展示保险公司应收订单
function showInsurerOrder() {
    window.location.href = "/ins/order/skipOrderList";
}
// 展示应收渠道订单
function showChannelOrder() {
    window.location.href = "/ins/order/skipOrderList?tabType=1";
}
function searchInsurer() {
    insurerOrdertable.ajax.reload();
}
function searchChannel() {
    channelOrdertable.ajax.reload();
}

$(".selectData").datepicker({
    autoclose: true, //自动关闭
    beforeShowDay: $.noop, //在显示日期之前调用的函数
    calendarWeeks: false, //是否显示今年是第几周
    clearBtn: false, //显示清除按钮
    daysOfWeekDisabled: [], //星期几不可选
    endDate: Infinity, //日历结束日期
    forceParse: true, //是否强制转换不符合格式的字符串
    format: 'yyyy-mm-dd', //日期格式
    keyboardNavigation: true, //是否显示箭头导航
    language: "zh-CN", //语言
    minViewMode: 0,
    orientation: "auto", //方向
    rtl: false,
    startDate: -Infinity, //日历开始日期
    startView: 0, //开始显示
    todayBtn: false, //今天按钮
    todayHighlight: true, //今天高亮
    weekStart: 0 //星期几是开始
});

function needpaid1() {
    var inschannel = $("#tabType").val();
    console.log("------------------+++++++++++++"+inschannel);
    if (inschannel == 0) {
        var insurerId1 = $("#insurerId1 option:selected").val();
        var plateNum1 = $("#plateNum1").val();
        var channelId1 = $("#channelId").val();
        var themeKey = $("#themeKey option:selected").val();
        var orderStatus = $("#orderStatus").val();
        console.log("++++++++++++++++++++++++++++++++" + inschannel)
        location.href = "/ins/order/insuranceNeedPay?insurerId1=" + insurerId1 + "&plateNum1=" + plateNum1 + "&channelId1=" + channelId1 + "&themeKey=" + themeKey + "&orderStatus=" + orderStatus + "&flag=" + "1";
        $("#needpaid").modal("hide");
    }
    if (inschannel == 1) {
        var insurerId2 = $("#insurerId2 option:selected").val();
        var plateNum2 = $("#plateNum2").val();
        var channelId2 = $("#channelId").val();
        //var dateIssuance=$("#dateIssuance").val();
        var insurantStartDate = $("#insurantStartDate").val();
        var insurantEndDate = $("#insurantEndDate").val()
        console.log("++++++++++++++++++++++++++++++++" + inschannel)
        location.href = "/ins/order/channelNeedPay?insurerId2=" + insurerId2 + "&plateNum2=" + plateNum2 + "&channelId2=" + channelId2 + "&insurantStartDate=" + insurantStartDate + "&insurantEndDate=" + insurantEndDate + "&flag=" + "3";
        $("#needpaid").modal("hide");
    }

}
function settleChannelRebate(tid, channelRebateAmt) {
    var date = $.format("yyyy-MM-dd", new Date());
    console.log(111, date);
    // $("#payDate").val(date);
    $("#tid").val(tid);
    $("#channelRebateAmt").val(channelRebateAmt);
    $("#channelMustRebateAmt").val(channelRebateAmt);
    $('#dwchannel').modal({backdrop: 'static', keyboard: false});
}
function payChannel() {
    if ($("#payChannelRebateAmt").submit().parsley().isValid()) {
        $.ajax({
            url: "/ins/order/payChannelOrder",
            type: "POST",
            data: $("#payChannelRebateAmt").serialize(),
            success: function (data) {
                if (data.success) {
                    $("#dwchannel").hide();
                }
            }
        })
    }
}
//控制tab页面的展示
var tab1 = $("#tab1");
var tab2 = $("#tab2");
var tab3 = $("#tab3");
var tab4 = $("#tab4");
var tab5 = $("#tab5");
var tab6 = $("#tab6");
var myTab = $("#myTab li");
var orderOperateRecords = $("#orderOperateRecords");
function controlTabShow(tabIndex) {
    myTab.eq(0).removeClass("active");
    myTab.eq(1).removeClass("active");
    myTab.eq(2).removeClass("active");
    myTab.eq(2).addClass("hidden");
    myTab.eq(3).removeClass("active");
    myTab.eq(3).addClass("hidden");
    myTab.eq(4).removeClass("active");
    myTab.eq(4).addClass("hidden");
    myTab.eq(5).removeClass("active");
    myTab.eq(5).addClass("hidden");
    tab1.removeClass(" active in");
    tab1.hide();
    tab2.removeClass(" active in");
    tab2.hide();
    tab3.removeClass(" active in");
    tab3.hide();
    tab4.removeClass(" active in");
    tab4.hide();
    tab5.removeClass(" active in");
    tab5.hide();
    tab6.removeClass(" active in");
    tab6.hide();
    orderOperateRecords.addClass("hidden");
    switch (tabIndex) {
        case "0":
            myTab.eq(tabIndex).addClass("active");
            tab1.addClass(" active in");
            tab1.show();
            break;
        case "1":
            myTab.eq(tabIndex).addClass("active");
            tab2.addClass(" active in");
            tab2.show();
            break;
        case "2":
            myTab.eq(tabIndex).addClass("active");
            myTab.eq(tabIndex).removeClass("hidden");
            tab3.addClass(" active in");
            tab3.show();
            break;
        case "3":
            myTab.eq(tabIndex).addClass("active");
            myTab.eq(tabIndex).removeClass("hidden");
            tab4.addClass(" active in");
            orderOperateRecords.removeClass("hidden");
            tab4.show();
            break;
        case "4":
            myTab.eq(tabIndex).addClass("active");
            myTab.eq(tabIndex).removeClass("hidden");
            tab5.addClass(" active in");
            orderOperateRecords.removeClass("hidden");
            tab5.show();
            break;
        case "5":
            myTab.eq(tabIndex).addClass("active");
            myTab.eq(tabIndex).removeClass("hidden");
            tab6.addClass(" active in");
            orderOperateRecords.removeClass("hidden");
            tab6.show();
            break;
        default:
            console.log(tabIndex);
    }
}
//查询所有渠道名称
$("#channel").keyup(function () {
    var name = $("#channel").val();
    if (name != null && name != "") {
        $("#ch").html("");
        $("#channelId").val("");
        $.ajax({
            url: "/channel/findAllChannelName/" + name,
            data: {},
            type: "get",
            success: function (data) {
                if (data.success) {
                    var channelIds = "";
                    var chHtml = "";
                    for (var i in data.result) {
                        if (data.result[i].companyRegisterName != null) {
                            chHtml += '<li value=' + data.result[i].tid + '><a href="javascript:;">' + data.result[i].companyRegisterName + '</a></li>';
                        } else {
                            chHtml += '<li value=' + data.result[i].tid + '><a href="javascript:;">' + data.result[i].contact.name + '</a></li>';
                        }
                        channelIds += "," + data.result[i].tid;
                    }
                    /*var width = parseInt($("#channel").width());*/
                    if (channelIds != null && channelIds != "") {
                        channelIds = channelIds.substring(1, channelIds.length);
                        $("#ch").parent().addClass("open");
                    } else {
                        channelIds = -1;
                        $("#ch").parent().removeClass("open");
                    }
                    $("#ch").html(chHtml);
                    $("#channelId").val(channelIds);
                    $("#ch li").click(function () {
                        $("#channel").val($(this).text());
                        $("#channelId").val($(this).val());
                    })

                } else {
                    $("#channelId").val(-1);
                    $("#ch").parent().removeClass("open");
                }
            },
            dataType: "json"
        })
    } else {
        $("#ch").parent().removeClass("open");
        $("#channelId").val("");
        $("#ch li").remove();
    }
})

$("#channels").keyup(function () {
    var name = $("#channels").val();
    if (name != null && name != "") {
        $("#chs").html("");
        $("#channelId").val("");
        $.ajax({
            url: "/channel/findAllChannelName/" + name,
            data: {},
            type: "get",
            success: function (data) {
                if (data.success) {
                    var channelIds = "";
                    var chHtml = "";
                    for (var i in data.result) {
                        if (data.result[i].companyRegisterName != null) {
                            chHtml += '<li value=' + data.result[i].tid + '><a href="javascript:;">' + data.result[i].companyRegisterName + '</a></li>';
                        } else {
                            chHtml += '<li value=' + data.result[i].tid + '><a href="javascript:;">' + data.result[i].contact.name + '</a></li>';
                        }
                        channelIds += "," + data.result[i].tid;
                    }
                    if (channelIds != null && channelIds != "") {
                        channelIds = channelIds.substring(1, channelIds.length);
                        $("#chs").parent().addClass("open");
                    } else {
                        channelIds = -1;
                        $("#chs").parent().removeClass("open");
                    }
                    $("#chs").html(chHtml);
                    $("#channelId").val(channelIds);
                    $("#chs li").click(function () {
                        $("#channels").val($(this).text());
                        $("#channelId").val($(this).val());
                    })

                } else {
                    $("#channelId").val(-1);
                    $("#chs").parent().removeClass("open");
                }
            },
            dataType: "json"
        })
    } else {
        $("#chs").parent().removeClass("open");
        $("#channelId").val("");
        $("#chs li").remove();
    }
})
// 支付功能的js
function passInsurer() {
    $('#payOrderForm').parsley().whenValidate({}).done(function () {
        $("#directReset6").modal().show();
    })
}
function surePay() {
    $("#payOrderForm").ajaxSubmit({
        url: "/ins/order/payOperate",
        type: "POST",
        success: function (data) {
            if (data.success) {
                var msgStr;
                if (data.result == null) {
                    msgStr = "产品信息不存在";
                } else if (data.result == "success") {
                    window.parent.location.href = "/ins/order/skipOrderList";
                } else {
                    var time1 = format(new Date(data.result.product_update_date), "yyyy-MM-dd hh:mm:ss");
                    msgStr = "产品信息已失效，失效日期是" + time1;
                    if (data.result.status == 1) {
                        $("#payStatus").val(3);
                    } else {
                        $("#payStatus").val(4);
                    }
                    $("#payFail").removeClass("hidden");
                    $("#continuePay").removeClass("hidden");
                    $("#cancel").addClass("hidden");
                    $("#confirm").addClass("hidden");
                    $("#me1").html(msgStr);
                    $("#modal-dialogs_one").modal("show");
                }
            } else {
                $("#payFail").addClass("hidden");
                $("#continuePay").addClass("hidden");
                $("#cancel").removeClass("hidden");
                $("#confirm").removeClass("hidden");
                $("#me1").html(data.msg);
                $("#modal-dialogs_one").modal("show");
            }
        }
    });
}
function continueOperate(payStatus) {
    $("#directReset6").modal().hide();
    if (payStatus == 2) {
        $("#payStatus").val(payStatus);
    } else if (payStatus == 3 || payStatus == 4) {
        surePayCancel();
        return;
    }
    surePay();
}
function payCanel() {
    $("#directReset7").modal().show();
}
function surePayCancel() {
    window.location.href = "/ins/order/skipOrderList";
}
function format(date, str) {
    var mat = {};
    mat.M = date.getMonth() + 1;//月份记得加1
    mat.H = date.getHours();
    mat.s = date.getSeconds();
    mat.m = date.getMinutes();
    mat.Y = date.getFullYear();
    mat.D = date.getDate();
    mat.d = date.getDay();//星期几
    mat.d = check(mat.d);
    mat.H = check(mat.H);
    mat.M = check(mat.M);
    mat.D = check(mat.D);
    mat.s = check(mat.s);
    mat.m = check(mat.m);
    console.log(typeof mat.D)
    if (str.indexOf(":") > -1) {
        return mat.Y + "-" + mat.M + "-" + mat.D + " " + mat.H + ":" + mat.m + ":" + mat.s;
    }
    if (str.indexOf("-") > -1) {
        return mat.Y + "-" + mat.M + "-" + mat.D + " " + mat.H + "：" + mat.m + "：" + mat.s;
    }
}
function check(str) {
    str = str.toString();
    if (str.length < 2) {
        str = '0' + str;
    }
    return str;
}
//审核页面js
/**
 * 审核驳回
 */
function resetOrder(type) {
    if (type == 1) {
        $("#directReset1").modal().show();
    } else {
        if ($("#chackOrderForm").parsley().validate("wizard-step-2")) {
            $("#directReset4").modal().show();
        }
    }
}
function insureReset(type) {
    var flag;
    var checkMsg;
    if (type == 1) {
        flag = $("#resetOrderCheck").submit().parsley().isValid();
        checkMsg = $("#checkMsg1").val();
    } else {
        flag = true;
        checkMsg = $("#remark1").val();
    }
    if (flag) {
        var ordId = $("#orderId").val();
        $.ajax({
            url: "/ins/order/checkReset",
            asyno: false,
            data: {"tid": ordId, remark: checkMsg},
            success: function (data) {
                if (data.success == false) {
                    alert(data.msg);
                } else {
                    window.location.href = "/ins/order/skipOrderList";
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
    }
}
/**
 * 计算全保费
 * @param typeInsurance 险种类型 0 交强险、1 商业险
 */
function calcAllPremium(typeInsurance) {
    var insurance = $("#insurance").val();
    var vehicleTexVal = $("#vehicleTex").val();
    $.ajax({
        url: "/ins/order/calcAllPremium",
        type: "POST",
        data: {"insurance": insurance, "vehicleTexVal": vehicleTexVal},
        success: function (data) {
            var allPremium = data.allPremium;
            $("#allPremiumSpan").val(allPremium);
            $("#allPremiumInput").val(allPremium);
        }
    })
}

/**
 * 计算渠道返点
 */
function calcRebateRate() {
    if ($("#chackOrderForm").parsley().validate("wizard-step-1")) {
        var checkOrderId = $("#checkOrderId").val();
        var productIdValue = $("#productId").val();
        var typeValue = $("#typeInsurance2").val();
        var paymentDaysValue = $("#laonAccount").val();
        var allPremiumValue = $("#allPremiumInput").val();
        var paySubjectValue = $("#themeKey2 option:selected").val();
        var followTicketPointValue = $("#followTicketPoint").val();
        var supplementPointValue = $("#supplementPoint").val();
        $.ajax({
            url: "/ins/order/getRebateAndMoney",
            type: "POST",
            data: {
                tid: checkOrderId,
                productId: productIdValue,
                type: typeValue,
                paymentDays: paymentDaysValue,
                allPremium: allPremiumValue,
                paySubject: paySubjectValue,
                followTicketPoint: followTicketPointValue,
                supplementPoint: supplementPointValue
            },
            async: false,
            success: function (data) {
                if (data.success) {
                    $("#costTotal").removeClass("hidden");
                    $("#passButton").removeAttr("disabled");
                    $("#calcRebateRateButton").attr("disabled", "disabled");
                    $("#resetOrderOne").addClass("hidden");
                    $("#channelLookPointSpan").val(data.result.channelRebate);
                    $("#channelLookPointInput").val(data.result.channelRebate);
                    $("#texPremiumSpan").val(data.result.texPremium);
                    $("#texPremiumInput").val(data.result.texPremium);
                    var s = parent.document.getElementById('iframeParent');

                    function setIframeHeight() {
                        s.style.height = document.body.offsetHeight + 'px';
                    }

                    setTimeout(function () {
                        setIframeHeight();
                    }, 200);
                } else {
                    if (data.result == null) {
                        $("#checkShowInfo").html(data.msg);
                        $("#directReset2").modal().show();
                    } else if (data.result.status == 1 || data.result.status == 2) {
                        $("#checkShowInfo1").html(data.msg);
                        $("#directReset3").modal().show();
                    } else {
                        $("#checkShowInfo").html(data.msg);
                        $("#directReset2").modal().show();
                    }
                    $("#costTotal").addClass("hidden");
                }
            }
        })
    }
}
function reloadHtml() {
    window.location.href = "/ins/order/skipOrderList";
}
/**
 * 多次计算渠道返点流程控制
 */
function reCalRebateRate() {
    $("#calcRebateRateButton").removeAttr("disabled");
    $("#passButton").attr("disabled", "disabled");
    $("#costTotal").addClass("hidden");
}
/**
 * 计算渠道返佣费和渠道应支付保费
 */
function calRebateFee() {
    var allP = $("#allPremiumInput").val();
    var channelPit = $("#channelPoint").val();
    var type = $("#typeInsurance2").val();
    $.ajax({
        url: "/ins/order/calRebateFee",
        type: "POST",
        data: {"allP": allP, "channelPit": channelPit, "type": type},
        success: function (data) {
            $("#channelPayPremiumInput").val(data.channelMustPay);
            $("#channelPayPremiumSpan").val(data.channelShowPay);
            $("#channelRebateFeeInput").val(data.channelRebate);
            $("#channelRebateFee").val(data.channelRebate)
        }
    })
}
/**
 * 审核通过
 */
function passOrder() {
    if ($('#chackOrderForm').submit().parsley().isValid()) {
        $("#directReset5").modal().show();
    }
}
function insurePassOrder() {
    $.ajax({
        url: "/ins/order/passOrder",
        async: false,
        type: "POST",
        data: $('#chackOrderForm').serialize(),
        success: function (data) {
            if (data.success == false) {
                alert(data.msg);
            } else {
                window.location.href = "/ins/order/skipOrderList";
            }
        }
    });
}
//完善保单页面js
function getParent() {
    $("#modal-dialog2", window.parent.document).modal().show();
}
window.ParsleyValidator.addValidator('inlength', function (value) {
    if (value != "") {
        var Innumber = $("#Innumber").val();
        console.log(Innumber.length);
        return value.length > Innumber.length;
    }
    return true;
}, 32);

function updateTime() {
    var datePolicy = $("#datePolicy").val();
    var datePolicyNum = new Date(datePolicy).getTime();
    var expireDateNum = datePolicyNum + 365 * 24 * 60 * 60 * 1000;
    var expireDate = new Date(expireDateNum);
    var expireDate1 = dateFtt("yyyy-MM-dd", expireDate);
    $("#expireDate").val(expireDate1);
}

function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

$("#showTab").click(function () {
    $('#form').parsley().whenValidate({}).done(function () {
        $.ajax({
            type: "post",
            url: "/ins/order/updateByOrdCode",
            data: $("#form").serialize(),
            success: function (msg) {
                if (msg.success == true) {
                    window.location.href = "/ins/order/skipOrderList";
                }
            }
        });
    });
});

window.ParsleyValidator.addValidator('only', function (ordNum) {
    var result = true;
    if (ordNum != "" && ordNum != null) {
        $.ajax({
            type: "post",
            url: "/ins/order/onlyOrdNum",
            async: false,
            data: {ordNum: ordNum},
            success: function (msg) {
                if (msg.success != true) {
                    result = false;
                }
            }
        });
    }
    return result;
}, 32);